home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / pctj8401.arc / WCDEBUG.C < prev   
Text File  |  1983-09-28  |  2KB  |  72 lines

  1. /*  Figure 2  - wc  with debugging printf statements */
  2.  
  3. /* wc.c - count words */
  4. #include <stdio.h>
  5. #define  WORD   0
  6. #define  NOT_WORD 1
  7.  
  8. FILE *fd ;
  9.  
  10. main(argc,argv)
  11.  int argc ;
  12.  char *argv[] ;
  13.  {
  14.    int c ;
  15.    long wc ;
  16.  
  17.    if( argc < 2  )
  18.      { printf("\n no name");
  19.        exit(0) ;
  20.      }
  21.    fd = fopen(argv[1],"r");      /* open the file */
  22.    if( fd == 0 )
  23.      { printf("\n can't open");
  24.        exit(0) ;
  25.      }
  26.    printf(" fd= %d \n",fd) ;
  27.  
  28.    wc = 0 ;
  29.    while( skip(NOT_WORD) != EOF )        /* skip to beginning of next word */
  30.      { 
  31.        printf("\n after skip(NOT_WORD) \n"); 
  32.        wc++ ;
  33.        if( skip(WORD) == EOF )           /* skip to end of the word */
  34.            break ;
  35.        printf("\n after skip(WORD) \n");
  36.        
  37.      } ;
  38.    fclose(fd) ;
  39.    printf("\n %ld words",wc);
  40.  }
  41.  
  42.  
  43. int check(c)           /* classify a char as part of word or not */
  44.  {
  45.     printf(" check-%x",c);
  46.        c = c & 0x7f ;
  47.        if( (c == ' ')
  48.         || (c == '\c')
  49.         || (c == '\n')
  50.         || (c == ',')
  51.         || (c == '.')
  52.         || (c == '(')
  53.         || (c == ')') )
  54.           return( NOT_WORD ) ;
  55.        else return( WORD ) ;
  56.  }
  57.  
  58. int skip(skip_type)    /* skip chars of skip_type in file fd */
  59.  int skip_type ;
  60.  {
  61.    int c ;
  62.  
  63.    printf("\n skip called - skip_type= %d \n",skip_type);
  64.    c = getc(fd) ;
  65.    while( (c != EOF) && (check(c) == skip_type ) )
  66.      { printf(" loop-%x",c) ;
  67.        c = getc(fd) ;
  68.      } ;
  69.    return( c ) ;
  70.  }
  71.  
  72.